<?php
/**
 * Created by PhpStorm.
 * User: mr.lee
 * Date: 2018/3/28
 * Time: 上午8:56
 * 卡券投放操作
 */

namespace app\common\controller;


use app\common\model\CardGivecardLog;

use app\common\model\CardMember;
use app\common\model\CardUserGet;
use app\common\model\Merchant;
use app\common\model\Paylog;
use app\common\model\PaymentCardPut;
use app\common\model\PaymentCardPutLog;
use think\Controller;


class AfterPayPutCard extends Wxoption
{

    //orderid
    public function paymentcard($arr){
        $Paylog = new Paylog();
        $param = $Paylog->where('orderid',$arr['orderid'])->field('mid,orderid,pay_fs,pay_cate,mid,price,openid,add_time,attach')->find();
        if ($param['pay_fs'] == 0 || $param['pay_fs'] == 2 && $param['pay_cate'] == "pay" && $param['attach'] == '') {
            $CardUserGet = new CardUserGet();
            $PaymentCardPut = new PaymentCardPut();
            $PaymentCardPutLog = new PaymentCardPutLog();
            $paycardlost = $PaymentCardPutLog->where('orderid', $param['orderid'])->field('id,putstatu')->find();
            $Merchant = new Merchant();
            $top_mid = $Merchant->where(['id' => $param['mid']])->value('top_mid');
            //END
            //查询领卡记录
            $map['top_mid'] = $top_mid;
            $map['card_type'] = 'MEMBER_CARD';
            $map['openid'] = $param['openid'];
            $map['fire'] = 1;
            $map['have'] = 1;
            $map['is_del'] = 1;
            $user = $CardUserGet->where($map)->field('card_id,code,id,top_mid,openid,nickname')->find();
            //END
            if (!$user){
                $mab['is_member'] = 'Y';        //不是有效会员的时候查找允许非会员参与的支付后送券
            }
            $mab['top_mid'] = $top_mid;
            $mab['statu'] = 1;
            $mab['start_time'] = ['lt', time()];
            $mab['end_time'] = ['gt', time()];
            $mab['min_fee'] = ['elt', $param['price']];
            $mab['max_fee'] = ['egt', $param['price']];
            $payment = $PaymentCardPut->where($mab)->order('max_fee desc')->find();
            //判断是否有支付后送券
            if ($payment) {
                if ($user) {
                    if ($paycardlost['putstatu'] == 1) {
                        //如果等于1则结束
                        $arrc = 'error2';
                    } else {
                        //如果领卡记录等于0
                        $data['mid'] = $param['mid'];
                        $data['orderid'] = $param['orderid'];
                        $data['card_id'] = $user['card_id'];
                        $data['user_code'] = $user['code'];
                        $data['openid'] = $param['openid'];
                        $data['fid'] = $payment['id'];
                        $data['price'] = $param['price'];
                        $data['paytime'] = $param['add_time'];
                        $data['putstatu'] = 1;
                        $PaymentCardPutLog->allowField(true)->save($data);
                        $give_rec_id = $PaymentCardPutLog->id;
                        //发放卡券
                        $arr['orderid'] = $param['orderid'];
                        $arr['card_id'] = $user['card_id'];
                        $arr['code'] = $user['code'];
                        $arr['openid'] = $param['openid'];
                        $arr['nickname'] = $user['nickname'];
                        $arr['mid'] = $param['mid'];
                        $arr['give_rec_id'] = $give_rec_id;
                        $arr['id'] = $user['id'];
                        $arr['top_mid'] = $user['top_mid'];
                        //余额更新
                        $arrc = $this->paymentputcard($arr, $payment);
                    }
                }else{
                    if ($paycardlost['putstatu'] == 1) {
                        //如果等于1则结束
                        $arrc = 'error2';
                    } else {
                        //如果领卡记录等于0
                        $data['mid'] = $param['mid'];
                        $data['orderid'] = $param['orderid'];
                        $data['card_id'] = "";
                        $data['user_code'] = "public";
                        $data['openid'] = $param['openid'];
                        $data['fid'] = $payment['id'];
                        $data['price'] = $param['price'];
                        $data['paytime'] = $param['add_time'];
                        $data['putstatu'] = 1;
                        $PaymentCardPutLog->allowField(true)->save($data);
                        $give_rec_id = $PaymentCardPutLog->id;
                        //发放卡券
                        $arr['orderid'] = $param['orderid'];
                        $arr['card_id'] = '';
                        $arr['code'] = '';
                        $arr['openid'] = $param['openid'];
                        $arr['nickname'] = '用户';
                        $arr['mid'] = $param['mid'];
                        $arr['give_rec_id'] = $give_rec_id;
                        $arr['id'] = $user['id'];
                        $arr['top_mid'] = $user['top_mid'];
                        //余额更新
                        $arrc = $this->paymentputcard($arr, $payment);
                    }
                }
            }else{
                $arrc = 'error2';
            }
        }else {
            $arrc = 'error3';
        }
        return $arrc;
    }

    //支付后送券
    public function paymentputcard($param,$payment){
        $Paylog = new Paylog();
        $payinfo = $Paylog->where(['orderid'=>$param['orderid']])->find();
        //是否发送过
        if ($payinfo['pay_is_put'] == 0 || $payinfo['pay_is_put'] == '0'){
            $Merchant = new Merchant();
            $top_mid = $Merchant->where(['id'=>$payinfo['mid']])->value('top_mid');
            $PaymentCardPut = new PaymentCardPut();
            $map['top_mid'] = $top_mid;
            $map['statu'] = 1;
            $CardMember = new CardMember();
            $CardUserGet = new CardUserGet();
            $cardus = $CardMember->where(['top_mid'=>$top_mid])->field('card_id')->find();
            $map1['card_id'] = $cardus['card_id'];
            $map1['card_type'] = 'MEMBER_CARD';
            $map1['openid'] = $param['openid'];
            $map1['fire'] = 1;
            $map1['have'] = 1;
            $map1['is_del'] = 1;
            $user = $CardUserGet->where($map1)->field('nickname,code,card_id')->find();
            if ($payment['num'] > $payment['get_num']){
                $card_no = count(json_decode($payment['zs_card_id_all'],true));
                if($card_no > 0){
                    $arr['zs_card_id_all'] = $payment['zs_card_id_all'];
                    $arr['openid'] = $payinfo['openid'];
                    $arr['code'] = !empty($user['code'])?$user['code']:"public";
                    $arr['mid'] = $payinfo['mid'];
                    $arr['card_id'] = $cardus['card_id'];
                    $arr['give_rec_id'] = $param['give_rec_id'];
                    $arr['top_mid'] = $top_mid;
                    $res = $this->payment_zs_cardslog($arr);
                    if ($res) {
                        $SendMsg = new TemplateMsg();
                        $arrs['first'] = '点击“详情”领取优惠券';
                        $arrs['mid'] = $arr['mid'];
                        $arrs['openid'] = $arr['openid'];
                        $arrs['card_id'] = '';
                        $arrs['name'] = !empty($user['nickname'])?$user['nickname']:'您好';
                        $arrs['title'] = $payment['zs_card_info'];
                        $arrs['give_id'] = $payment['id'];
                        $arrs['type'] = 'paymentcard';
                        $arrs['url'] = '/mobile.php?s=/cardget/getpaymentcards/mid/' . $arr['mid'] . '/rec_id/' . $arr['give_rec_id'];
                        $resu = $SendMsg->send_tpl('put_gift', $arrs);
                    } else {
                        $resu = '';
                    }
                    $resut['card'] = $resu;
                    $datac['giveinfo'] = json_encode($resut, JSON_UNESCAPED_UNICODE);
                    $datac['paystatu'] = 1;
                    $datac['paytime'] = time();
                    $PaymentCardPutLog = new PaymentCardPutLog();
                    $PaymentCardPutLog->allowField(true)->save($datac,['id'=>$param['give_rec_id']]);
                    $PaymentCardPut->where(['id'=>$payment['id']])->setInc('get_num');
                } else {
                    $resut['card'] = '无券赠送';
                }
            }else{
                $resut = 'error3';
            }

        }else{
            $resut = 'error1';
        }
        return $resut;
    }

    //生成赠卡记录[支付后
    /*
     * zs_card_id_all / card_id  /code /  give_rec_id  / mid / top_mid  / put_type
     * */
    public function payment_zs_cardslog($arr){
        $CardGivecardLog = new CardGivecardLog();
        $zs_card_id = $arr['zs_card_id_all'];
        if ($zs_card_id){
            $zs_card_id = json_decode($arr['zs_card_id_all'],true);

            $data = [];
            foreach ($zs_card_id as $k=>$v) {
                if (fileoffcards($v['zs_card_id']) == true) {
                    $data[$k]['mid'] = $arr['mid'];
                    $data[$k]['card_id'] = $arr['card_id'];
                    $data[$k]['code'] = $arr['code'];
                    $data[$k]['openid'] = $arr['openid'];
                    $data[$k]['zs_card_id'] = $v['zs_card_id'];
                    $data[$k]['give_rec_id'] = $arr['give_rec_id'];
                    $data[$k]['mid'] = $arr['mid'];
                    $data[$k]['top_mid'] = $arr['top_mid'];
                    $data[$k]['put_sume'] = $v['zs_no'];
                    $data[$k]['put_type'] = 'payment_card';
                    $data[$k]['create_time'] = time();
                }
            }

            $res = $CardGivecardLog->saveAll($data);
        }else{
            $res = '';
        }
        return $res;
    }

}